package 力扣日常刷题.木23一月.第14天314;

import 力扣日常刷题.ListNode;

/**
 * @author 帅小伙
 * @date 2022/3/14
 * @description
 */
public class Demo63合并链表 {

    public ListNode mergeKLists(ListNode[] lists) {
        ListNode p = null;
        for(int i = 0; i < lists.length; i++) {
            p = merge(p,lists[i]);
        }
        return p;
    }

    public ListNode merge(ListNode a,ListNode b) {
        if(a == null || b == null) return a == null ? b : a;
        if(a.val < b.val) {
            a.next = merge(a.next,b);
            return a;
        }else{
            b.next = merge(a,b.next);
            return b;
        }
    }

}
